package com.lemon.smartsafety.ui.activity.safe.data;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MenuItem;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.entity.event.base.AppEvent;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafety.ui.fragment.safe.data.CompanySearchResultFragment;
import com.lemon.smartsafety.ui.fragment.safe.data.SafeDataCollectionMainFragment;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Cooliean on 16/1/24.
 * 安全巡查主界面 就是企业列表
 */
public class SafeDataCollectionMainActivity extends BaseSearchFragmentActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(SafeDataCollectionMainActivity.class);

    private SafeDataCollectionMainFragment mFragment;
    private CompanySearchResultFragment    mResultFragment;


    @Override
    public Fragment getFragment() {
        if (mFragment != null) {
            return mFragment;
        }
        mFragment = SafeDataCollectionMainFragment.newInstance();
        return mFragment;
    }

    public Fragment getResultFragment(String query){
        Bundle bundle = new Bundle();
        bundle.putString(CompanySearchResultFragment.KEY_SEARCH, query);
        mResultFragment = CompanySearchResultFragment.newInstance();
        mResultFragment.setArguments(bundle);
        return mResultFragment;
    }

    @Override
    public int getSearchHintRes() {
        return R.string.search_company_hit;
    }


    @Subscribe
    public void onEventMainThread(AppEvent event) {
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.safe_title);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                AppManager.getAppManager().finishActivity(SafeDataCollectionMainActivity.this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_content, getResultFragment(query), getResultFragment(query).getClass().getName()).commit();
        return super.onQueryTextSubmit(query);
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (mFragment != null) {
            mFragment.doSearch(newText);
        }
        return super.onQueryTextChange(newText);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
